import re

from flask import Blueprint,request,make_response,jsonify,redirect
from flask_restful import reqparse
from util.myjwt import myjwt
from util.captcha.captcha import captcha
import json, random, time, requests
from util.myredis import r
from util.smssdk import send_message
from models.user import *

classidy_blue = Blueprint("classidyblue",__name__)


# @classidy_blue.route('/getmenu')
# def getmenu():
#     classify = db.session.execute("select * from classify where pid=0")
#     # type = classify.query.filter_by(pid=0).all()
#
#     # type = list(type)
#     # print(type)
#     clist = list(classify)
#
#
#     print(clist)
#     list1 =[]
#     for i in clist:
#         list1.append({'id':i[0],'name':i[3]})
#     print(list1)
#     return jsonify({'code':200,'data':list1})

@classidy_blue.route('/getone')
def getone():
    clasify =  db.session.execute("select * from classify")
    clist = list(clasify)
    list1 = []
    menulist = []
    for i in clist:
        if i[4] == 0:
            if i[4] not in menulist:
                list1.append({'id':i[0],'name':i[3],'children':[]})
                menulist.append([i[0]])
    for j in clist:
        for index,p in enumerate(list1):
            if j[4] == p['id']:
                list1[index]['children'].append({'id':j[0],'name':j[3],'pid':j[4],'children1':[]})
    for x in clist:
        for index,p in enumerate(list1):
            aa = list1[index]['children']
            for index,t in enumerate(aa):
                if x[4] == t['id']:
                    aa[index]['children1'].append({'id':x[4],'name':x[3]})
    print(list1)
    return jsonify({'code':200,'data':list1})


@classidy_blue.route('/setbasic',methods=['POST'])
def setbasic():
    data = request.data
    if data:
        data = json.loads(data)
        userid = data['userid']
        name = data['name']
        sex = data['sex']
        identity = data['identity']
        addr = data['addr']
        email = data['email']
        bas = basic.query.filter_by(userid=userid).first()
        if bas:
            bas.sex = sex
            bas.name = name
            bas.email = email
            bas.identity = identity
            bas.addr = addr
            db.session.add(bas)
            db.session.commit()
            return jsonify({'code':200,'msg':'修改成功'})
        bsc = basic(name=name,sex=sex,identity=identity,addr=addr,email=email,userid=userid)
        db.session.add(bsc)
        db.session.commit()
        return jsonify({'code':200,'msg':'ok'})
    else:
        return jsonify({'code':300,'msg':'输入信息'})
# 查找当先用户信息是否存在
@classidy_blue.route('/getbasic')
def getbasic():
    userid = request.args.get('userid')
    bas = basic.query.filter_by(userid=userid).first()
    print(bas.id)
    list=[]
    list.append({'id':bas.id,'name':bas.name,'sex':bas.sex,'identity':bas.identity,'addr':bas.addr,'email':bas.email})

    print(list)
    return jsonify({'code':200,'list':list})

@classidy_blue.route('/setserve',methods=['POST'])
def setserve():
    data = request.data
    if data:
        data = json.loads(data)
        userid = data['userid']
        ability = data['ability']
        skil = data['skil']
        pay = data['pay']
        close = data['close']
        work = data['work']
        worktime = data['worktime']
        bas = serve.query.filter_by(userid=userid).first()
        if bas:
            bas.ability = ability
            bas.skil = skil
            bas.close = close
            bas.pay = pay
            bas.work = work
            bas.worktime = worktime
            db.session.add(bas)
            db.session.commit()
            return jsonify({'code':200,'msg':'修改成功'})
        bsc = serve(ability=ability,skil=skil,pay=pay,close=close,work=work,worktime=worktime)
        db.session.add(bsc)
        db.session.commit()
        return jsonify({'code':200,'msg':'ok'})
    else:
        return jsonify({'code':300,'msg':'输入信息'})
# 查找当先用户信息是否存在
@classidy_blue.route('/getserve')
def getserve():
    userid = request.args.get('userid')
    bas = serve.query.filter_by(userid=userid).first()
    list=[]
    list.append({'id':bas.id,'ability':bas.ability,'skil':bas.skil,'close':bas.close,'pay':bas.pay,'work':bas.work,'worktime':bas.worktime})
    print(list)
    return jsonify({'code':200,'list':list})

@classidy_blue.route('/gettypes')
def gettypes():
    type1 = types.query.all()
    list = []
    mlist = []
    for i in type1:
        if i.pid == 0:
            if i.id not in mlist:
                list.append({'id':i.id,'name':i.name,'son':[]})
                mlist.append([i.id])

    for j in type1:
        for index,p in enumerate(list):
            if j.pid == p['id']:
                list[index]['son'].append({'id':j.id,'oname':j.name,'pid':j.pid,'sons':[]})
    for x in type1:
        for index,p in enumerate(list):
            aa =list[index]['son']
            for indexs,t in enumerate(aa):
                if x.pid == t['id']:
                    aa[indexs]['sons'].append({'id':x.id,'tname': x.name})

    print(list)

    return jsonify({'code':200,'list':list})













# 发布信息
